package servlet;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.CommentService;
import service.JobService;
import service.MovieService;
import service.RatingService;
import service.UserService;
import util.RegexUtil;

import domain.ActorJob;
import domain.CrewJob;
import domain.Movie;
import domain.User;
import ex.UnparsableToDoubleException;
import ex.UnparsableToIntegerException;

public class MovieProfile extends HttpServlet {
	MovieService movieService = new MovieService();
	CommentService commentService = new CommentService();
	RatingService ratingService = new RatingService();
	UserService uservice = new UserService();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		try {
			String action = request.getParameter("which");
			if (action != null) {
				if (RegexUtil.isParsableToInt(action)) {
					long mid = Long.parseLong(action);
					domain.Movie movie = movieService.getMovieByMid(mid);
					if (movie != null) {
						List<User> director = new LinkedList<User>();
						director.add(movie.getUser());
						List<User> actor = uservice.getUsersActed(mid);
						Map<User,String> crew = uservice.getUsersWorked(mid);

						request.setAttribute("director", director);
						request.setAttribute("actor", actor);
						request.setAttribute("crew", crew);
						
						request.setAttribute("comments", commentService.getCommentsByMid(mid));

						request.setAttribute("movie", movie);
					} else {
						response.sendRedirect("home");
					}

				} else {
					response.sendRedirect("home");
				}
			} else {
				response.sendRedirect("home");
			}

			if (request.getAttribute("movie") != null) {

				domain.User user=null;
				HttpSession session = request.getSession();
				if (session.getAttribute("user") != null) {
					user = (domain.User) session
							.getAttribute("user");
					request.setAttribute("user", user);
				}
				
				Movie movie = (Movie) request.getAttribute("movie");
				if (movie.isCompleted()) {
					if(user!=null && user.getUid()==movie.getUser().getUid())
						request.setAttribute("hasRatedBefore", true);
					else if(user!=null){
						request.setAttribute("hasRatedBefore", ratingService.hasRatedBefore(user,movie));
					}
					
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("jsp/user/movie-show.jsp");
					if (dispatcher != null)
						dispatcher.forward(request, response);

				} else {
					
					if(user!=null){
						JobService jService = new JobService();
						List<ActorJob> actors = jService
								.getActorJobsByMovie(movie);
						List<CrewJob> crews = jService.getCrewJobsByMovie(movie);

						request.setAttribute("actorjobs", actors);
						request.setAttribute("crewjobs", crews);

						request.setAttribute("comments",
								commentService.getCommentsByMid(movie.getMid()));// TODO

						RequestDispatcher dispatcher = request
								.getRequestDispatcher("jsp/user/show-project.jsp");
						if (dispatcher != null)
							dispatcher.forward(request, response);
					} else {
						// login ol ulan
						response.sendRedirect("home");
					}
				}
			} else {
				response.sendRedirect("home");
			}
		}
		// catch (DatabaseException e) {
		// request.setAttribute("error", e.getMessage());
		// }
		catch (Exception e) {
			request.setAttribute("error", "A general error occured");

		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		String genre = request.getParameter("genre");
		String year = request.getParameter("year");
		String runTimeStr = request.getParameter("runTime");
		String summary = request.getParameter("summary");
		// String image=request.getParameter("image");TODO
		String poster = request.getParameter("poster");
		String linkToMovie = request.getParameter("link");
		String ratingStr = request.getParameter("rating");
		String popularityStr = request.getParameter("popularity");

		/*
		 * try { RegexUtil.isParsableToInt(runTimeStr); } catch
		 * (UnparsableToIntegerException e1) { // TODO Auto-generated catch
		 * block e1.printStackTrace(); } try {
		 * RegexUtil.isParsableToInt(popularityStr); } catch
		 * (UnparsableToIntegerException e1) { // TODO Auto-generated catch
		 * block e1.printStackTrace(); } try {
		 * RegexUtil.isParsableToDouble(ratingStr); } catch
		 * (UnparsableToDoubleException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */// TODO_BURAK uncomment

		int runTime = Integer.parseInt(runTimeStr);
		int popularity = Integer.parseInt(popularityStr);
		double rating = Double.parseDouble(ratingStr);

		/*
		 * Movie movie=new
		 * Movie((long)-1,name,genre,year,summary,runTime,poster,
		 * linkToMovie,rating,popularity,false,null,null);
		 * 
		 * movieService.createMovie(movie);
		 * 
		 * HttpSession session = request.getSession();
		 * session.setAttribute("movie", movie);
		 */// TODO_BURAK uncomment

		response.sendRedirect("movie?who=dmyMovie");

		// RequestDispatcher dispatcher =
		// request.getRequestDispatcher("jsp/user/register.jsp");
		// if (dispatcher != null) dispatcher.forward(request, response);
	}

}